package com.xbb.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.xbb.entity.DailyLog;
import com.xbb.entity.Student;
import com.xbb.entity.Teacher;
import com.xbb.entity.dto.DailyLogDto;
import com.xbb.service.DailyLogService;
import com.xbb.service.StatisticService;
import com.xbb.service.StudentService;
import com.xbb.service.TeacherService;
import com.xbb.util.Result;

@RestController
public class DailyLogController {

	@Autowired
	DailyLogService dailyLogService;
	@Autowired
	StudentService studentService;
	@Autowired
	TeacherService teacherService;
	@Autowired
	StatisticService statisticService;
	
	@RequestMapping("/getInfoById")
	public Object getInfoById(int id,int type) {
		//System.out.println(id);
		//System.out.println(type);
		if(type==0) {
			Student stuById = studentService.getStuById(id);
			if(stuById!=null) {
				return new Result(0, "", stuById);
			}else {
				return new Result(1, "此用户不存在", null);
			}
		}else {
			Teacher teaById = teacherService.getTeaById(id);
			if(teaById!=null) {
				return new Result(0, "", teaById);
			}else {
				return new Result(1, "此用户不存在", null);
			}
		}
	}
	
	
	@RequestMapping("/getDailyLogList")
	public Object getDailyLogList(DailyLogDto dailyLogDto, ModelMap map) {
		List<DailyLogDto> list = dailyLogService.getAllDailyLog(dailyLogDto);
		dailyLogDto.setLimit(null);
		dailyLogDto.setPage(null);
		List<DailyLogDto> list2 = dailyLogService.getAllDailyLog(dailyLogDto);
		return new Result(0, "", list2.size(), list);
	}
	
	@RequestMapping("/addLog")
	public Object addLog(String uid,int rid) {
		DailyLog dl=new DailyLog();
		dl.setCardNo(uid);
		dl.setReadRoomId(rid);
		System.out.println(dl);
		List<DailyLogDto> checkUser = dailyLogService.checkUser(uid);
		System.out.println(checkUser.size());
		if(checkUser.size()<=0) {
			if(dailyLogService.addLog(dl)) {
				//更新访问资源人数
				//statisticService.updateStaInfoNum(rid);
				return new Result(0,"添加成功");
			}else {
				return new Result(1,"添加失败");
			}
		}else {
			return new Result(1,"添加失败,此用户正在占用其他资源");
		}
		
	}
	
	@RequestMapping("/updateLog")
	public Object updateLog(int rid) {
		DailyLog d=new DailyLog();
		d.setLogid(rid);
		if(dailyLogService.updateLog(d)) {
			return new Result(0,"修改成功");
		}else {
			return new Result(1,"修改失败");
		}
	}
	
	
	
}
